package change

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
)

// ==================== 商户信息变更 ====================

// MerchInfoChangeRequest 商户信息变更请求
type MerchInfoChangeRequest struct {
	models.OrgBaseRequest
	// BaseInfo 商户基础信息
	BaseInfo map[string]interface{} `json:"base_info,omitempty"`
	// BusinessInfo 商户业务信息
	BusinessInfo map[string]interface{} `json:"business_info,omitempty"`
	// SettleInfo 结算信息
	SettleInfo map[string]interface{} `json:"settle_info,omitempty"`
	// IdentificationInfo 商户身份信息
	IdentificationInfo map[string]interface{} `json:"identification_info,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewMerchInfoChangeRequest 创建商户信息变更请求
func NewMerchInfoChangeRequest() *MerchInfoChangeRequest {
	req := &MerchInfoChangeRequest{}
	return req
}

// GetSensitiveFields 返回需要加密的敏感字段路径列表
func (r *MerchInfoChangeRequest) GetSensitiveFields() []string {
	// 与Python版本保持一致，定义敏感字段路径
	return []string{
		"identification_info.id_card_name",         // 经营者/法人姓名
		"identification_info.id_card_english_name", // 经营者/法人英文名称
		"identification_info.id_card_no",           // 经营者/法人身份证件号码
		"identification_info.legal_person_phone",   // 经营者/法人手机号码
	}
}

// SetBaseInfo 设置商户基础信息
func (r *MerchInfoChangeRequest) SetBaseInfo(baseInfo map[string]interface{}) *MerchInfoChangeRequest {
	r.BaseInfo = baseInfo
	return r
}

// SetBusinessInfo 设置商户业务信息
func (r *MerchInfoChangeRequest) SetBusinessInfo(businessInfo map[string]interface{}) *MerchInfoChangeRequest {
	r.BusinessInfo = businessInfo
	return r
}

// SetSettleInfo 设置结算信息
func (r *MerchInfoChangeRequest) SetSettleInfo(settleInfo map[string]interface{}) *MerchInfoChangeRequest {
	r.SettleInfo = settleInfo
	return r
}

// SetIdentificationInfo 设置商户身份信息
func (r *MerchInfoChangeRequest) SetIdentificationInfo(identificationInfo map[string]interface{}) *MerchInfoChangeRequest {
	r.IdentificationInfo = identificationInfo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *MerchInfoChangeRequest) SetRawRequestMap(rawMap map[string]interface{}) *MerchInfoChangeRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *MerchInfoChangeRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *MerchInfoChangeRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.BaseInfo != nil && len(r.BaseInfo) > 0 {
		baseMap["base_info"] = r.BaseInfo
	}
	if r.BusinessInfo != nil && len(r.BusinessInfo) > 0 {
		baseMap["business_info"] = r.BusinessInfo
	}
	if r.SettleInfo != nil && len(r.SettleInfo) > 0 {
		baseMap["settle_info"] = r.SettleInfo
	}
	if r.IdentificationInfo != nil && len(r.IdentificationInfo) > 0 {
		baseMap["identification_info"] = r.IdentificationInfo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *MerchInfoChangeRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// MerchInfoChangeResponse 商户信息变更响应
type MerchInfoChangeResponse struct {
	models.StandardResponse
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// ApplyID 业务申请编号
	ApplyID string `json:"apply_id,omitempty"`
}

// NewMerchInfoChangeResponse 创建商户信息变更响应
func NewMerchInfoChangeResponse() *MerchInfoChangeResponse {
	return &MerchInfoChangeResponse{}
}
